package com.jason.mvptest.presenter;

import android.os.Handler;

import com.jason.mvptest.bean.User;
import com.jason.mvptest.implement.login.ClickLoginListener;
import com.jason.mvptest.implement.login.IUserLonginEvent;
import com.jason.mvptest.implement.login.UserLoginEvent;
import com.jason.mvptest.view.UserLoginView;

/**
 * @Author: Jason
 * @Date: 2016/11/10
 * @Time: 16:34.
 */

public class UserLoginPresenter  {

    private IUserLonginEvent mIUserLonginEvent;
    private UserLoginView mUserLoginView;
    private Handler mHandler = new Handler();

    public UserLoginPresenter(UserLoginView userLoginView) {
        this.mUserLoginView = userLoginView;
        mIUserLonginEvent = new UserLoginEvent();

    }

    public void login() {
        mUserLoginView.showLoading();
        mIUserLonginEvent.login(mUserLoginView.getUserName(), mUserLoginView.getPassWord(), new ClickLoginListener() {
            @Override
            public void loginSusscess(final User user) {
                mHandler.post(new Runnable() {
                    @Override
                    public void run() {
                        mUserLoginView.toMainActivity(user);
                        mUserLoginView.hideLoading();
                    }
                });
            }

            @Override
            public void loginFaild() {
                mHandler.post(new Runnable() {
                    @Override
                    public void run() {
                        mUserLoginView.hideLoading();
                        mUserLoginView.loginErrorShow();
                    }
                });
            }
        });
    }

    public void clear() {
        mUserLoginView.clearUserName();
        mUserLoginView.clearPassWord();
    }


}
